package cn.edu.scau.serviceImpl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.dao.PeopleDao;
import cn.edu.scau.daoImpl.PeopleDaoImpl;
import cn.edu.scau.entity.People;
import cn.edu.scau.entity.Work;
import cn.edu.scau.service.PeopleService;

/**
 * 部门任务信息的业务逻辑层
 * 
 * @author cxywan
 *
 */
@Service("peopleServiceImpl")
public class PeopleServiceImpl implements PeopleService<People> {

	/**
	 * 持久层
	 */
	@Autowired
	private PeopleDao PeopleDao;

	public PeopleDao getPeopleDao() {
		return PeopleDao;
	}

	public void setPeopleDao(PeopleDao peopleDao) {
		PeopleDao = peopleDao;
	}

	/**
	 * 保存部门人物实体
	 * 
	 * @param entity
	 *            People实体
	 * @param file
	 *            头像
	 * @param fileName
	 *            头像文件名
	 * @param path
	 *            头像保存路径
	 * 
	 */
	public void add(People entity) {

		// 保存任务信息
		this.getPeopleDao().save(entity);
	}

	/**
	 *
	 * 删除部门人物实体
	 *
	 * @param entity
	 *            People实体
	 */
	public boolean delete(People entity, Integer id) {

		if (this.getPeopleDao().find(People.class, id) != null) {
			this.getPeopleDao().delete(entity);
			return true;
		}
		return false;

	}

	/**
	 *
	 * 删除部门人物实体
	 *
	 * @param id
	 *            People id
	 */
	@Override
	public boolean delete(Integer id) {

		if (this.getPeopleDao().find(People.class, id) != null) {
			this.getPeopleDao().delete(People.class, id);
			return true;
		}

		return false;

	}

	/**
	 * 更新部门人物实体
	 * 
	 * @param entity
	 *            People实体
	 * @param file
	 *            头像
	 * @param fileName
	 *            头像名
	 * @param path
	 *            头像保存路径
	 * 
	 */
	@Override
	public boolean update(People entity, Integer id) {

		if (this.getPeopleDao().find(People.class, id) != null) {
			this.getPeopleDao().update(entity);
			return true;
		}
		return false;

	}

	/**
	 * 根据id获取部门人物实体
	 * 
	 * @param id
	 *            人物id
	 * @return 人物实体
	 */
	@Override
	public People getPeopleByID(Integer id) {
		return this.getPeopleDao().find(People.class, id);
	}

	/**
	 * 根据columnID（第几届），来计算部门人物个数
	 * 
	 * @param columnID
	 *            栏目ID
	 * 
	 * @return
	 */
	public Long findPageByColumnID(Integer columnID) {

		return this.getPeopleDao().findCountByColumnID(columnID);
	}

	/**
	 * 获取页面总数
	 * 
	 * @param columnID
	 *            栏目ID
	 * @param pageSize
	 *            每一页要展示的条目总数
	 * @param frontOrBack
	 *            前台还是后台获取
	 */
	public long countTheEntityNum(Integer columnID, Integer pageSize, boolean frontOrBack) {

		Long count = this.getPeopleDao().findCountByColumnID(columnID);

		return count % pageSize == 0 ? count / pageSize : count / pageSize + 1;

	}

	/**
	 * 获取前后或者后台的people属性
	 * 
	 * @param columnID
	 *            栏目ID
	 * @param pageNO
	 *            第几页
	 * @param pageSize
	 *            每一页要展示的条目总数
	 * @param frontOrBack
	 *            前台还是后台获取
	 */
	public List getPeopleProperty(Integer columnID, String department, Boolean frontOrBack) {

		if (frontOrBack) {
			return this.getPeopleDao().findPeopleByColumnIDAndDepartment(columnID, department);
		} else {
			return this.getPeopleDao().getPropertyofPeopleManagement(columnID);
		}
	}

	/**
	 * 保存头像
	 * 
	 * @param file
	 *            头像
	 * @param fileName
	 *            头像名
	 * @param path
	 *            头像保存地址
	 */
	public String saveImage(MultipartFile file, HttpServletRequest request, String releaseDate) {

		// 获取保存头像文件夹的真实路径
		String path = request.getSession().getServletContext().getRealPath("/Upload/Upload/Photo/");
		// 组合图像的名称
		String fileName = this.getDate(releaseDate, 0) + this.getRandomString(10)
				+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
		// 新建一个文件
		File targetFile = new File(path, fileName);
		// 如果文件夹不存在，则新建文件夹
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 保存人物头像
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 返回头像保存路径名
		return "/Upload/Upload/Photo/" + fileName;

	}

	/**
	 * 根据日期，生成头像名称的部分
	 *
	 * @return
	 */
	public String getDate(String releaseDate, int start) {

		char[] chs = releaseDate.toCharArray();
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < chs.length; i++) {

			if (chs[i] == ' ') {
				break;
			}

			if (chs[i] >= '0' && chs[i] <= '9') {
				builder.append(chs[i]);
			}
		}
		return new String(builder);
	}

	/**
	 * 获取随机字符串，用于组成图片的名字
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) { // length表示生成字符串的长度
		String base = "0123456789012345678901234567890123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	@Override
	public List showTheEntity(String type, Integer columnId, Integer id, Integer pageNo, boolean frontOrBackground) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> saveImage(MultipartFile[] files, HttpServletRequest request, String releaseDate) {
		// TODO Auto-generated method stub
		return null;
	}


}
